local skel = fk.CreateSkill {
  name = "emo__kunlong",
}

Fk:loadTranslationTable{
  ["emo__kunlong"] = "困龙",
  [":emo__kunlong"] = "你可以将点数最大或最小的一张手牌当【铁索连环】使用，此牌生效后若目标不为横置，其摸一张牌；为横置：其需对自己造成1点冰冻伤害，否则你令其一个技能无效直到其重置。",

  ["#emo__kunlong"] = "困龙：将点数最大或最小的一张手牌当【铁索连环】使用",
  ["#emo__kunlong_damage"] = "对自己造成1点冰冻伤害",
  ["#emo__kunlong_invalidate"] = "你一个技能无效直到重置",
  ["#emo__kunlong-invalidate"] = "困龙：选择 %src 一个技能，无效直到其重置",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "iron_chain",
  prompt = "#emo__kunlong",
  mute_card = false,
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    local hand = player:getCardIds("h")
    if #selected == 0 and table.contains(hand, to_select) then
      local num = Fk:getCardById(to_select).number
      local min, max = num, num
      for _, id in ipairs(hand) do
        local n = Fk:getCardById(id).number
        max = math.max(max, n)
        min = math.min(min, n)
      end
      return num == max or num == min
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("iron_chain")
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_response = function (self, player, response)
    return not response
  end,
})

skel:addEffect(fk.CardEffectFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player == data.to and not player.dead and table.contains(data.card.skillNames, skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player.chained then
      local choices = {"#emo__kunlong_damage", "#emo__kunlong_invalidate"}
      local choice = room:askToChoice(player, {choices = choices, skill_name = skel.name})
      if choice == "#emo__kunlong_damage" then
        room:damage({
          from = player,
          to = player,
          damage = 1,
          damageType = fk.IceDamage,
        })
      else
        local skills = player:getSkillNameList()
        local from = data.from
        if #skills > 0 and from and not from.dead then
          local skill = room:askToChoice(from, {
            choices = skills, prompt = "#emo__kunlong-invalidate:" .. player.id,
            detailed = true, skill_name = skel.name
          })
          room:addTableMark(player, "emo__kunlong_record", skill)
        end
      end
    else
      player:drawCards(1, skel.name)
    end
  end,
})

skel:addEffect(fk.ChainStateChanged, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("emo__kunlong_record") ~= 0 and not player.chained
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__kunlong_record", 0)
  end,
})

skel:addEffect("invalidity", {
  invalidity_func = function (self, from, skill)
    return table.contains(from:getTableMark("emo__kunlong_record"), skill:getSkeleton().name)
  end,
})

return skel
